package trinidad;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class NetThread extends Thread{

	Object what;
	ServerSocket serverSocket;

	
	public NetThread(Object what, ServerSocket serverSocket) {
		super();
		this.what = what;
		this.serverSocket = serverSocket;
	}
	
	public void run(){
		//wysylanie
		Socket clientSocket = getClientSocket(serverSocket);
		
		ObjectOutputStream oos;
		try {
			oos = new ObjectOutputStream(clientSocket.getOutputStream());
			oos.writeObject(what);
			
			//ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
			
			//Object dostal = ois.readObject();
			
			oos.close();
			//ois.close();
			
			clientSocket.close();
			//serverSocket.close();
			//return dostal;
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("Wyslalem");
	}
	
	private Socket getClientSocket(ServerSocket s) {
			try {
				while(s.accept()==null){
					 try {
						sleep((int)(Math.random()*1000));
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				return s.accept();
			} catch (IOException e) {
				System.err.println("Accept failed.");

				return null;
			}
	}
	/*
	private Socket getClientSocket(ServerSocket s) {
		try {
			return s.accept();
		} catch (IOException e) {
			System.err.println("Accept failed.");
			return null;
		}
	}
	*/


}
